'''
Created on Mar 13, 2012

@author: wilshusensk
'''

import os
from java.lang import System, Class, ClassNotFoundException
from java.sql import DriverManager, SQLException
import sys
import string
import urllib
from CarryOn import *

def createTable(tableName, fields):
    cmd = "CREATE TABLE " + tableName +"(StudentID INT, LastName CHAR(20), FirstName CHAR(15))"
    stat.execute(cmd)

def insertToTable(tableName, fields):
    cmd = "INSERT INTO " + tableName +" VALUES ("
    for i in range(len(fields)):
        if(i > 0):
            cmd += ","
        cmd += "'"
        cmd += fields[i]
        cmd += "'"
    cmd += ")"
    print(cmd)
    stat.execute(cmd)
    
def dropTable(tableName):
    cmd = "DROP TABLE " + tableName
    stat.execute(cmd)

def entryToTable(tableName, name, declination, rightAscention):
    cmd = "INSERT INTO NameTable VALUES ('" + name + "', '" + declination + "', '" + rightAscention + "')"
        #cmd = "INSERT INTO NameTable VALUES (", `name`, ", ", `declination`, ", ", `rightAscention`, ")"
    #1st arg can't be coerced to String
    stat.execute(cmd)
    
def selectFrom(tableName, columns):
    if(columns[0] == "*"):
        cmd = "SELECT * FROM " + tableName
    else:
        cmd = "SELECT "
        for i in range(len(columns)):
            if(i > 0):
                cmd += ","
            cmd += "'"
            cmd += columns[i]
            cmd += "'"
        cmd += " FROM " + tableName

def setupDatabase():
    try:
        System.setProperty("sqlite.purejava", "true")
        Class.forName("org.sqlite.JDBC")
        con = DriverManager.getConnection("jdbc:sqlite:constellationInfo")
        global stat
        stat = con.createStatement()

    except ClassNotFoundException, e:
        sys.exit(e.message)
    except SQLException, e:
        sys.exit(e.message)
        
